package sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import sys.core.ResponseResult;
import sys.dao.CustomerClaimMapper;
import sys.dao.CustomerMapper;
import sys.entity.CustomerEntity;
import sys.entity.CustomerOperationEntity;
import sys.entity.PageData;
import sys.entity.User;
import sys.entity.dto.CustomerClaimDto;
import sys.entity.dto.CustomerDto;
import sys.service.CustomerService;
import sys.utils.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {


    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerClaimMapper customerClaimMapper;


    @Override
    public PageData<CustomerEntity> likeSelect(CustomerDto customerDto, HttpServletRequest request) {
        LambdaQueryWrapper<CustomerEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件：客户名称
        if (customerDto.getName() != null && !customerDto.getName().equals("")) {
            lambdaQueryWrapper.like(CustomerEntity::getName, customerDto.getName());
        }
        //条件:意向
        if (customerDto.getIntention() != null) {
            lambdaQueryWrapper.eq(CustomerEntity::getIntention, customerDto.getIntention());
        }
        //条件:部门ID
        if (customerDto.getDepartmentId() != null) {
            lambdaQueryWrapper.eq(CustomerEntity::getDepartmentId, customerDto.getDepartmentId());
        }
        if (customerDto.getType() == 1) {
            //公共客户
            lambdaQueryWrapper.eq(CustomerEntity::getCustomerUser, -1);
        } else {
            User user = SessionUtil.getUser(request);
            lambdaQueryWrapper.eq(CustomerEntity::getCustomerUser, user.getId());
        }
        //排序
        if (customerDto.getOrder() != null) {
            if (customerDto.getOrder() == 1) {
                lambdaQueryWrapper.orderByAsc(CustomerEntity::getIntention);
            } else {
                lambdaQueryWrapper.orderByDesc(CustomerEntity::getIntention);
            }
        }

        IPage<CustomerEntity> page = new Page<CustomerEntity>(customerDto.getCurrentPage() == null ? 1 : customerDto.getCurrentPage(), customerDto.getPageSize() == null ? 10 : customerDto.getPageSize());
        IPage<CustomerEntity> pageInfo = customerMapper.selectPage(page, lambdaQueryWrapper);
        PageData<CustomerEntity> pageData = new PageData<>();
        pageData.setResult(pageInfo.getRecords());
        pageData.setTotalPage((int) pageInfo.getPages());
        pageData.setCurrentPage((int) pageInfo.getCurrent());
        pageData.setCount((int) pageInfo.getTotal());

        return pageData;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult claim(CustomerClaimDto customerClaimDto, HttpServletRequest request) {


        //获取用户信息
        User user = SessionUtil.getUser(request);
        if (user == null) {
            return ResponseResult.failed("未获取到当前用户信息");
        }
        //修改 把-1改成办理人id
        CustomerEntity customerEntity = new CustomerEntity();
        customerEntity.setId(customerClaimDto.getCustomerId());
        customerEntity.setCustomerUser(Integer.valueOf(user.getId()));
        customerMapper.updateById(customerEntity);
        //添加操作
        CustomerOperationEntity customerOperationEntity = new CustomerOperationEntity();
        customerOperationEntity.setCustomerId(customerClaimDto.getCustomerId());
        //保存当前办理人的名称
        customerOperationEntity.setProcessingName(user.getUserName());
        customerOperationEntity.setDepartmentId(customerClaimDto.getDepartmentId());
        customerOperationEntity.setProcessingType(customerClaimDto.getType());
        customerOperationEntity.setProcessingTime(new Date());
        customerOperationEntity.setProcessingId(Integer.valueOf(user.getId()));
        customerOperationEntity.setProcessingPhone(user.getPhone());

        int i = customerClaimMapper.insert(customerOperationEntity);
        if (i > 0) {
            return ResponseResult.success("认领成功！");
        }

        return ResponseResult.failed("认领失败！");
    }

    @Override
    public ResponseResult detail(Integer customerId) {
        //参数校验
        if (customerId == null) {
            return ResponseResult.failed("参数不能为空");
        }
        LambdaQueryWrapper<CustomerOperationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerOperationEntity::getCustomerId, customerId);
        List<CustomerOperationEntity> operationEntityList = customerClaimMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(operationEntityList)) {
            return ResponseResult.failed("数据为空");
        }
        return ResponseResult.success(operationEntityList);
    }

}
